<template>
  <div id="questions-app">
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="addClick">新增</el-button>
      </el-col>
      <el-col :span="12">
        <el-input @blur="iptBlur" placeholder="请输入内容" v-model="text">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="seach"
          ></el-button>
        </el-input>
      </el-col>
      <el-table
        :data="examList"
        border
        style="width: 100%; margin-top: 50px"
        :height="heights"
      >
        <el-table-column label="#" type="index" width="50"></el-table-column>
        <el-table-column
          prop="title"
          label="题目"
          width="100"
        ></el-table-column>
        <el-table-column prop="type" label="分类" width="180"></el-table-column>
        <el-table-column  label="作者" width="180">xuchen</el-table-column>
        <el-table-column
          prop="date"
          label="发布时间"
          width="150"
          sortable
        ></el-table-column
        >
        <el-table-column prop="address" label="操作" width="250">
          <template slot-scope="scope">
            <el-button type="info" size="mini" @click="detail(scope.row)"
              >详情</el-button
            >
            <el-button size="mini" type="warning" @click="updateXG(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-pagination
      style="margin-top: 20px"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 15, 20, 30]"
      :pageSize="10"
      :total="10"
      
    ></el-pagination>
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      size="60%"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="题目" height="30" prop="title">
          <el-input
            :disabled="see"
            v-model="form.title"
            placeholder="测试题目"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="cid">
          <el-select
            :disabled="see"
            v-model="form.cid"
            placeholder="请选择类别"
          >
            <template v-for="(v, i) in classify">
              <el-option
                :key="i"
                v-if="v.id"
                :label="v.name"
                :value="v.id"
              ></el-option>
            </template>
          </el-select>
        </el-form-item>
        <el-form-item label="选项" prop="options">
          <el-row class="display-flex">
            <!-- 选项 答案 -->
            <el-col :span="20">
              <el-row v-for="(v, i) in form.options" :key="i">
                <el-col :span="2">
                  <el-checkbox :disabled="see" v-model="v.checked">{{
                    v.index
                  }}</el-checkbox>
                </el-col>
                <el-col :span="22">
                  <el-input
                    :disabled="see"
                    type="textarea"
                    autosize
                    v-model="v.value"
                  ></el-input>
                </el-col>
              </el-row>
            </el-col>
            <!-- + - 按钮 -->
            <el-col :span="4" class="circle-btn">
              <div>
                <el-button
                  :disabled="see"
                  type="success"
                  icon="el-icon-plus"
                  circle
                  @click="addChoice"
                ></el-button>
                <el-button
                  :disabled="see"
                  type="danger"
                  icon="el-icon-minus"
                  circle
                  @click="reduceChoice"
                ></el-button>
              </div>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="答案">{{ answersArr }}</el-form-item>
        <el-form-item label="答案提示" prop="tips">
          <el-input
            :disabled="see"
            v-model="form.tips"
            placeholder="答案提示"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="see" @click="onSubmit('form')"
            >保存</el-button
          >
          <el-button @click="cancelClk">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
import mixins from "../../mixins";
import { mapActions, mapState, mapMutations } from "vuex";
export default {
  mixins: [mixins],
  data() {
    return {
      see: false,
      title: "新增信息",
      tableData: [],
      text: "",
      title: "新增",
      answersArr: [],
      form: {
        options: [
          {
            index: "A",
          },
          {
            index: "B",
          },
          {
            index: "C",
          },
          {
            index: "D",
          },
        ],
      },
      drawer: false,
      direction: "rtl",
    };
  },
  async mounted() {
    await this.getlist();
    await this.getlistx();
  },
  methods: {
    ...mapActions("question", [
      "getlistx",
      "addExam",
      "updateList",
      "deleteList",
    ]),
    ...mapActions("classify", ["getlist"]),
    ...mapMutations("question", ["SET_PAGE", "SET_PAGSIZE"]),

    // 新增
    addClick() {
      this.title = "新增信息";
      this.drawer = true;
      this.form = {
        options: [
          {
            index: "A",
          },
          {
            index: "B",
          },
          {
            index: "C",
          },
          {
            index: "D",
          },
        ],
      };
    },

    // 删除
    handleDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          await this.deleteList(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 分页
    async changFun(age) {
      await this.SET_PAGE(age);
      this.getlistx();
    },
    async handleSizeChange(val) {
      await this.SET_PAGSIZE(val);
      this.getlistx();
    },

    // 失去焦点
    iptBlur() {
      if (this.text == "") {
        this.getlistx();
      }
    },

    // 搜索
    seach() {
      this.getlistx(this.text);
    },

    // 详情
    detail(row) {
      this.title = "详情信息";
      this.drawer = true;
      this.see = true;
      if (typeof row.options == "string") {
        row.options = JSON.parse(row.options);
      }
      this.form = { ...row };
    },

    //修改
    updateXG(row) {
      this.title = "修改信息";
      this.drawer = true;
      if (typeof row.options == "string") {
        row.options = JSON.parse(row.options);
      }
      this.form = { ...row };
    },
    //////////  fenhao; maohao: yinhao' shuanyinhao" juhao. douyhao,
    // 提交
    onSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.form.id) {
            this.updateList({
              form: this.form,
              answers: this.answersArr,
              uid: this.$store.state.users.uid,
            });
            this.$message({
              message: "修改成功",
              type: "success",
            });
          } else {
            await this.addExam({
              form: this.form,
              answers: this.answersArr,
              uid: this.$store.state.users.uid,
            });
            this.$message({
              message: "新增成功",
              type: "success",
            });
          }
          this.drawer = false;
          this.form = {
            options: [
              {
                index: "A",
              },
              {
                index: "B",
              },
              {
                index: "C",
              },
              {
                index: "D",
              },
            ],
          };
        }
      });
      this.see = false;
    },

    // 增加选择项
    addChoice() {
      let word = String.fromCharCode(this.form.options.length + 65);
      this.form.options.push({ index: word });
    },

    // 减少选择
    reduceChoice() {
      if (this.form.options.length <= 4) {
        this.$message({
          message: "至少有四个选项",
          type: "warning",
        });
        return;
      }
      this.form.options.pop();
    },

    // 取消
    cancelClk() {
      this.drawer = false;
      this.see = false;
    },

    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
          this.see = false;
        })
        .catch((_) => {});
    },
  },
  created() {
    this.getlist();
    this.getlistx();
  },
  watch: {
    "form.options": {
      deep: true,
      handler(val) {
        this.answersArr = [];
        let a = val.map((v) => {
          if (v.checked) {
            this.answersArr.push(v.index);
          }
        });
      },
    },
  },
  computed: {
    ...mapState("question", ["examList", "total", "pageSize"]),
    ...mapState("classify", ["classify"]),
  },
};
</script>

<style scoped>
#questions-app {
  padding: 20px;
  height: 100%;
}
.display-flex {
  display: flex;
}

.circle-btn {
  align-self: flex-end;
  margin-left: 15px;
}

/* /deep/ .cell {
  height: 80px !important;
} */
</style>
